package cn.xlbweb.cli.controller;

import cn.xlbweb.cli.common.ServerResponse;
import cn.xlbweb.cli.common.TableResponse;
import cn.xlbweb.cli.config.CliOperateLog;
import cn.xlbweb.cli.common.constant.OperationConsts;
import cn.xlbweb.cli.model.User;
import cn.xlbweb.cli.pojo.dto.UserInsertDTO;
import cn.xlbweb.cli.pojo.dto.UserUpdateDTO;
import cn.xlbweb.cli.pojo.dto.UsersListDTO;
import cn.xlbweb.cli.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: bobi
 * @date: 2019-02-04 00:55
 * @description:
 */
@Api(value = "用户API接口")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @CliOperateLog(module = "用户模块", type = OperationConsts.GET, description = "查询当前登录用户")
    @ApiOperation(value = "查询当前登录用户", notes = "查询当前登录用户")
    @GetMapping("getUser")
    public ServerResponse getUser() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.success("查询成功", user);
    }

    @CliOperateLog(module = "用户模块", type = OperationConsts.GET, description = "分页模糊查询用户")
    @ApiOperation(value = "分页模糊查询用户", notes = "分页模糊查询用户")
    @RequiresPermissions("user:listUsers")
    @GetMapping("listUsers")
    public TableResponse listUsers(UsersListDTO usersListDTO,
                                   @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return iUserService.listUsers(usersListDTO, pageNum, pageSize);
    }

    @ApiOperation(value = "添加用户")
    @RequiresPermissions("user:insertUser")
    @PostMapping("insertUser")
    public ServerResponse insertUser(UserInsertDTO userInsertDTO) {
        return iUserService.insertUser(userInsertDTO);
    }

    @ApiOperation(value = "更新用户")
    @RequiresPermissions("user:updateUser")
    @PutMapping("updateUser")
    public ServerResponse updateUser(UserUpdateDTO userUpdateDTO) {
        return iUserService.updateUser(userUpdateDTO);
    }

    @ApiOperation(value = "删除用户")
    @RequiresPermissions("user:deleteUser")
    @DeleteMapping("deleteUser")
    public ServerResponse deleteUser(String ids) {
        return iUserService.deleteUser(ids);
    }
}
